<?php

class Products_content extends CI_Model{
	
	public function getProducts($start, $num){
		$this->db->select('*');
        $this->db->limit($num, $start);
		$this->db->order_by(field('id'), 'DESC');
		return $this->db->get(table('product'));
	}
	
    public function getProductNum(){
        $objDb = $this->getAllProducts();
        return $objDb->num_rows();
    }
	
    public function getAllProducts(){
        $this->db->select('*');
        $this->db->from(table('product'));
		$this->db->order_by(field('id'), 'DESC');
		return $this->db->get();
    }
	
    /*public function getProductById($id) {
        $this->db->select('*');
        $this->db->where(field('id'),$id);
		$result = $this->db->get(table('product'));
		$result = $result->result_array();
		return $result[0];
    }*/
	
	//Remove user
	function delete($data){
			$this->db->delete(table('product'), $data); 
		}
	//Add new user
	public function addnew(){
		$this->load->library("form_validation");
			
			$query_str = array(
						'name' => $this->input->post('name'),
						'broken_rate' => $this->input->post('broken_rate'),
						'shipping' => $this->input->post('shipping'),
						'mini_order' => $this->input->post('mini_order'),
						'payment_term' => $this->input->post('payment_term'),
						'fob_price' => $this->input->post('fob_price'),
						'description' => $this->input->post('description'),
						'image' => $this->input->post('image')
						);
			return $this->db->insert(table('product'),$query_str);
		}
	//edit user
	function edit() {
			$id = $this->uri->segment('4');
			$this->db->select('*');
			$this->db->from(table('product'));
			$this->db->where('id', $id);
			
			return $this->db->get();
		}
	//update user
	function update(){
			$this->load->library("form_validation");
			$query_str = array(
				'name' => $this->input->post('name'),
				'broken_rate' => $this->input->post('broken_rate'),
				'shipping' => $this->input->post('shipping'),
				'mini_order' => $this->input->post('mini_order'),
				'payment_term' => $this->input->post('payment_term'),
				'fob_price' => $this->input->post('fob_price'),
				'description' => $this->input->post('description'),
				'image' => $this->input->post('image')
				);
			$id = $this->input->post('getid');
			$this->db->where('id',$id);
			return $this->db->update(table('product'),$query_str);
		}
		//select full
		public function productDetail(){
			$id = $this->uri->segment(4);
			$this->db->select('*');
			$this->db->from(table('product'));
			$this->db->where('id', $id);
			return $this->db->get();
			}
		
}